package com.bls.productmall.controller;

import com.bls.productmall.entity.Order;
import com.bls.productmall.enums.OrderStatus;
import com.bls.productmall.service.OrderService;
import com.bls.productmall.vo.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@RestController
@RequestMapping("/api/order-info")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
    * 查询订单列表
    */
    @GetMapping("/list")
    public R list(){
        List<Order> list = orderService.listOrderByCreateTimeDesc();
        return R.ok().data("orderList", list);
    }

    /**
    * 根据用户id和用户是否删除来 查询订单列表
    * */
    @GetMapping("/userOrderList/{userId}")
    public R userOrderList(@PathVariable Long userId){
        List<Order> list = orderService.listOrderByUserIdAndUserDelete(userId);
        return R.ok().data("userOrderList", list);
    }

    /**
     * 用户根据订单id删除订单（只是将user_delete改为0）
     * */
    @GetMapping("/userDeleteOrder/{orderNo}")
    public R userDeleteOrder(@PathVariable String orderNo){
        Order order = orderService.userDeleteOrder(orderNo);
        return R.ok().setMessage("删除订单成功").data("order", order);
    }

    /**
     * 查询本地订单状态
     */
    @GetMapping("/query-order-status/{orderNo}")
    public R queryOrderStatus(@PathVariable String orderNo){
        String orderStatus = orderService.getOrderStatus(orderNo);
        if(OrderStatus.SUCCESS.getType().equals(orderStatus)){
            return R.ok().setMessage("支付成功"); //支付成功
        }
        return R.ok().setCode(101).setMessage("支付中......");
    }
}
